package com.example.mydemoproject.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mydemoproject.model.entity.QuartzJob;
import com.example.mydemoproject.model.request.QuartzJobReqVo;
import com.example.mydemoproject.model.response.Result;
import com.example.mydemoproject.service.impl.QuartzJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author LiuZhiYong
 * @description
 * @date 2022-11-25
 **/
@RestController
@RequestMapping("/quartzJob")
@Slf4j
@Api(tags = "定时任务接口")
@RequiredArgsConstructor
public class QuartzJobController {

    private final QuartzJobService quartzJobService;

    /**
     * 任务列表查询接口
     *
     * @param pageNo   当前页数
     * @param pageSize 每页展示条数
     * @return Result<Page < QuartzJob>>
     * @author liuzhiyong
     * @date 2022/11/25
     */
    @ApiOperation(value = "任务列表查询接口")
    @GetMapping("/lsit")
    public Result<Page<QuartzJob>> queryPageList(@ApiParam(name = "pageNo", value = "当前页数", required = true)
                                                 @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                 @ApiParam(name = "pageSize", value = "每页显示条数", required = true)
                                                 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<QuartzJob> page = new Page<>(pageNo, pageSize);
        Page<QuartzJob> resultList = quartzJobService.page(page);
        return Result.OK(resultList);
    }

    /**
     * 通过id查询定时任务信息
     *
     * @param id 定时任务id
     * @return com.example.mydemoproject.model.response.Result<com.example.mydemoproject.model.entity.QuartzJob>
     * @author liuzhiyong
     * @date 2022/11/25
     */
    @ApiOperation(value = "通过id查询任务信息")
    @GetMapping("/queryById")
    public Result<QuartzJob> queryQuartzJobById(@ApiParam(name = "id", value = "定时任务id", required = true)
                                                @RequestParam(name = "id") String id) {
        QuartzJob quartzJob = quartzJobService.getById(id);
        return Result.OK(quartzJob);
    }

    /**
     * 添加定时任务接口
     *
     * @param quartzJobReqVo 定时任务参数
     * @return com.example.mydemoproject.model.response.Result<Object>
     * @author liuzhiyong
     * @date 2022/11/25
     */
    @ApiOperation(value = "添加定时任务接口")
    @PostMapping("/add")
    public Result<Object> add(@RequestBody @Validated QuartzJobReqVo quartzJobReqVo) {
        quartzJobService.addQuartzJob(quartzJobReqVo);
        return Result.OK();
    }

    /**
     * 编辑定时任务
     *
     * @param quartzJobReqVo 定时任务信息
     * @return com.example.mydemoproject.model.response.Result<Object>
     * @author liuzhiyong
     * @date 2022/11/25
     */
    @ApiOperation(value = "编辑定时任务接口")
    @PostMapping("/edit")
    public Result<Object> edit(@RequestBody @Validated QuartzJobReqVo quartzJobReqVo) {
        quartzJobService.editQuartzJob(quartzJobReqVo);
        return Result.OK();
    }

    /**
     * 删除并停止定时任务接口
     *
     * @param id 任务id
     * @return com.example.mydemoproject.model.response.Result<Object>
     * @author liuzhiyong
     * @date 2022/11/25
     */
    @ApiOperation(value = "删除并停止定时任务接口")
    @GetMapping("/remove")
    public Result<Object> remove(@ApiParam(name = "id", value = "定时任务id", required = true)
                                 @RequestParam(name = "id") String id) {
        quartzJobService.removeQuartzJob(id);
        return Result.OK();
    }

    /**
     * 暂停定时任务接口
     *
     * @param id 任务id
     * @return com.example.mydemoproject.model.response.Result<Object>
     * @author liuzhiyong
     * @date 2022/11/25
     */
    @ApiOperation(value = "暂停定时任务接口")
    @GetMapping("/pause")
    public Result<Object> pauseJob(@ApiParam(name = "id", value = "定时任务id", required = true)
                                   @RequestParam(name = "id") String id) {
        quartzJobService.pauseJob(id);
        return Result.OK();
    }

    /**
     * 恢复定时任务
     *
     * @param id 任务id
     * @return com.example.mydemoproject.model.response.Result<Object>
     * @author liuzhiyong
     * @date 2022/11/25
     */
    @ApiOperation(value = "恢复定时任务接口")
    @GetMapping("/resume")
    public Result<Object> resumeJob(@ApiParam(name = "id", value = "定时任务id", required = true)
                                    @RequestParam(name = "id") String id) {
        quartzJobService.resumeJob(id);
        return Result.OK();
    }

    /**
     * 执行一次定时任务接口
     *
     * @param id    定时任务id
     * @return com.example.mydemoproject.model.response.Result<java.lang.Object>
     * @author liuzhiyong
     * @date 2022/11/26
     */
    @ApiOperation(value = "执行一次定时任务接口")
    @GetMapping("/excute")
    public Result<Object> excute(@ApiParam(name = "id", value = "定时任务id", required = true)
                                 @RequestParam(name = "id") String id) {
        quartzJobService.excute(id);
        return Result.OK();
    }

}
